package com.aaa.tmr.util;

import com.aaa.tmr.entity.OperLog;
import com.aaa.tmr.entity.User;
import com.aaa.tmr.service.OperLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;

import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;

/**
 * @author ：Marko
 * @date ：Created in 2021/3/22 18:50
 * @description ：记录增删改日志
 * @modified By：
 * @version:
 */
@Aspect
@Component
public class OperLogUtil {

    @Resource
    private OperLogService operLogService;

    @Resource
    private HttpSession session;

    /**
     * description: <br>
     *
     * @param joinpoint
     * @author Marko
     * @Date 2021/3/22 19:03
     **/
    @AfterReturning(pointcut = "execution(* com.aaa.tmr.service.impl.*.insert*(..)) " +
            "|| execution(* com.aaa.tmr.service.impl.*.update*(..))" +
            "|| execution(* com.aaa.tmr.service.impl.*.delete*(..))")
    public void recordOperLog(JoinPoint joinpoint) {
        OperLog operLog = new OperLog();
        //记录日志主题
        operLog.setTitle("商标注册");
        //记录日志操作类型
        String methodName = joinpoint.getSignature().getName();
        int bussinessType = BussinessConstants.OperBussinessType.DELETE;
        if (methodName.contains("insert")) {
            bussinessType = BussinessConstants.OperBussinessType.ADD;
        } else if (methodName.contains("update")) {
            bussinessType = BussinessConstants.OperBussinessType.UPDATE;
        }
        operLog.setBusinessType(bussinessType);
        //带包的类名称 获取类的限定名
        String className = joinpoint.getTarget().getClass().getName();
        operLog.setMethod(className + "." + methodName);
        //操作类别，0其它 1后台用户 2手机端用户
        operLog.setOperatorType(1);
        //获取当前登录人信息
        User user = (User) session.getAttribute("userInfo");
        operLog.setOperName(user.getUserName());
        //登录人部门
        operLog.setDeptName("研发部门");
        //获取HttpServletRequest
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        //获取uri
        String requestURI = request.getRequestURI();
        operLog.setOperUrl(requestURI);
        //获取IP
        String remoteAddr = request.getRemoteAddr();
        operLog.setOperIp(remoteAddr);
        //操作位置
        operLog.setOperLocation("郑州");
        //操作状态
        operLog.setStatus(0);
        //操作时间
        operLog.setOperTime(new Date());
        //参数
        /*String params = Arrays.toString(joinpoint.getArgs());
        operLog.setOperParam(params);*/
        Object[] args = joinpoint.getArgs();
        if(args!=null&&args.length>0){
            operLog.setOperParam(args[0].getClass().getSimpleName());
        }
        operLogService.insert(operLog);
    }
}
